
package com.yunfei.api.user;
import com.yunfei.api.user.contants.UpdateSystemVersionApiConstant;
import com.yunfei.api.user.contants.UserCenterContants;
import com.yunfei.api.user.result.*;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.GetStatusMapper;
import com.yunfei.mapper.aiiidb.TPersonalUserMapper;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.service.ValidMobileAndCodeService;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 用户中心
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = UserCenterContants.USER_CENTER_API_NAMESPACE,tags = UserCenterContants.USER_CENTER_API_NAMESPACE )
@RestController
@RequestMapping("/api/version")
public class UpdateSystemVersionApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private GetStatusMapper getStatusMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private ValidMobileAndCodeService validMobileAndCodeService;
    @Autowired
    private Environment env;

    @GetMapping(value = "/get",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = UserCenterContants.API_GET_APP_VERSION,notes =  UserCenterContants.AUTHOR_NOTE,position = 3,response = UserInfoResult.class)
    public ResultModel getUserInfo(String appid,String version,String uuid,String deviceFlg,String plus){
        ResultModel resultModel=new ResultModel();
        try{
            System.out.println("appid==========="+appid+",version====="+version+"设备类型==="+deviceFlg+",uuid===="+uuid);
            System.out.println("plus=================="+plus);
            AppVersionResult queryParam=new AppVersionResult();
            if(plus.contains("iOS")){
                queryParam.setDeviceType(UpdateSystemVersionApiConstant.DEVICE_IOS_FLG);
            }else{
                queryParam.setDeviceType(UpdateSystemVersionApiConstant.DEVICE_ANDROID_FLG);
            }
         /*   if(!StringUtil.isNullOrEmpty(deviceFlg)){
                queryParam.setDeviceType(deviceFlg.toUpperCase());
            }else{
                queryParam.setDeviceType(UpdateSystemVersionApiConstant.DEVICE_IOS_FLG);
            }*/
            AppVersionResult appVersionResult=tPersonalUserMapper.getAppVersion(queryParam);
            String appVersion=appVersionResult.getVersion();
            System.out.println("appVersion=================="+appVersion);
           if(compareVersion(appVersion,version)>0){
                    appVersionResult.setIsUpdate(1);
            }else{
                appVersionResult.setIsUpdate(0);
            }
            resultModel.setData(appVersionResult);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }

        return resultModel;
    }
    /**
    * @Description: 获取更新日志信息
    * @return: com.yunfei.bo.result.ResultModel
    * @Author: zhengyunfei
    * @Date: 2019/6/4
    * @time:10:35
    */
    @GetMapping(value = "/getVersionInfo",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = UserCenterContants.API_GET_APP_VERSION,notes =  UserCenterContants.AUTHOR_NOTE,position = 3,response = UserInfoResult.class)
    @ResponseBody
    public ResultModel getInfo(String device){
        ResultModel resultModel=new ResultModel();
        try{
            AppVersionResult queryParam=new AppVersionResult();
            queryParam.setDeviceType(device.toUpperCase());
            AppVersionResult appVersionResult=tPersonalUserMapper.getAppVersion(queryParam);
            resultModel.setData(appVersionResult);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    public static void main(String args[]){

       String res="{\"isReady\":true,\"tools\":{\"__UUID__\":5,\"UNKNOWN\":-1,\"IOS\":0,\"ANDROID\":1,\"platform\":0,\"debug\":0},\"bridge\":{\"NO_RESULT\":0,\"OK\":1,\"CLASS_NOT_FOUND_EXCEPTION\":2,\"ILLEGAL_ACCESS_EXCEPTION\":3,\"INSTANTIATION_EXCEPTION\":4,\"MALFORMED_URL_EXCEPTION\":5,\"IO_EXCEPTION\":6,\"INVALID_ACTION\":7,\"JSON_EXCEPTION\":8,\"ERROR\":9,\"callbacks\":{\"plus21562143984225\":{},\"plus41562143984387\":{}},\"commandQueue\":[],\"isInEvalJs\":0,\"synExecXhr\":{},\"execIframe\":null,\"execImg\":null},\"obj\":{},\"accelerometer\":{},\"audio\":{\"ROUTE_SPEAKER\":0,\"ROUTE_EARPIECE\":1},\"barcode\":{\"QR\":0,\"EAN13\":1,\"EAN8\":2,\"AZTEC\":3,\"DATAMATRIX\":4,\"UPCA\":5,\"UPCE\":6,\"CODABAR\":7,\"CODE39\":8,\"CODE93\":9,\"CODE128\":10,\"ITF\":11,\"MAXICODE\":12,\"PDF417\":13,\"RSS14\":14,\"RSSEXPANDED\":15},\"cache\":{},\"camera\":{},\"console\":{},\"contacts\":{\"ADDRESSBOOK_PHONE\":0,\"ADDRESSBOOK_SIM\":1},\"downloader\":{\"__taskList__\":[]},\"fingerprint\":{},\"gallery\":{\"__galleryStatus\":0,\"onPickImageFinished\":null},\"geolocation\":{\"lastPosition\":null},\"io\":{\"PRIVATE_WWW\":1,\"PRIVATE_DOC\":2,\"PUBLIC_DOCUMENTS\":3,\"PUBLIC_DOWNLOADS\":4},\"android\":{\"__Tool\":{\"undefObjectHash\":{}}},\"ios\":{\"__Tool\":{\"undefObjectHash\":{}}},\"maps\":{\"MapType\":{\"MAPTYPE_SATELLITE\":\"MAPTYPE_SATELLITE\",\"MAPTYPE_NORMAL\":\"MAPTYPE_NORMAL\"},\"SearchPolicy\":{\"TRANSIT_TIME_FIRST\":\"TRANSIT_TIME_FIRST\",\"TRANSIT_TRANSFER_FIRST\":\"TRANSIT_TRANSFER_FIRST\",\"TRANSIT_WALK_FIRST\":\"TRANSIT_WALK_FIRST\",\"TRANSIT_FEE_FIRST\":\"TRANSIT_FEE_FIRST\",\"DRIVING_TIME_FIRST\":\"DRIVING_TIME_FIRST\",\"DRIVING_NO_EXPRESSWAY\":\"DRIVING_NO_EXPRESSWAY\",\"DRIVING_FEE_FIRST\":\"DRIVING_FEE_FIRST\"},\"__bridge__\":{\"callback\":[]}},\"messaging\":{\"TYPE_SMS\":1,\"TYPE_MMS\":2,\"TYPE_EMAIL\":3},\"nativeUI\":{},\"nativeObj\":{},\"navigator\":{\"__statusBarHeight__\":20},\"oauth\":{},\"orientation\":{},\"payment\":{},\"key\":{},\"proximity\":{},\"push\":{},\"runtime\":{\"arguments\":\"\",\"version\":\"1.4.3\",\"innerVersion\":\"1.9.9.64805\",\"uniVersion\":\"2.0.1\",\"launchLoadedTime\":0,\"launcher\":\"default\",\"origin\":\"default\",\"processId\":\"13176\",\"startupTime\":1562143983801,\"appid\":\"__UNI__F2E5F92\",\"channel\":\"\"},\"share\":{},\"speech\":{},\"statistic\":{},\"storage\":{},\"stream\":{},\"ui\":{\"__nviews__\":{}},\"uploader\":{\"__taskList__\":{}},\"video\":{},\"webview\":{\"__JSON_Window_Stack\":{\"__UNI__F2E5F92\":{\"__view_array__\":[],\"__IDENTITY__\":\"NWindow\",\"__uuid__\":\"__UNI__F2E5F92\",\"__callbacks__\":{\"back\":[null]},\"__callback_id__\":\"plus21562143984225\",\"id\":\"__UNI__F2E5F92\"}}},\"net\":{},\"zip\":{},\"shortvideo\":{},\"ibeacon\":{},\"bluetooth\":{},\"sqlite\":{},\"device\":{\"imei\":\" \",\"imsi\":[],\"model\":\"iPhone6\",\"vendor\":\"Apple\",\"uuid\":\"32A1F07C-E24C-42D6-84CF-4FCADEC35416\"},\"os\":{\"language\":\"zh-Hans-CN\",\"version\":\"12.3.1\",\"name\":\"iOS\",\"vendor\":\"Apple\"},\"screen\":{\"height\":1334,\"width\":750,\"resolutionHeight\":667,\"resolutionWidth\":375,\"scale\":2,\"dpiX\":326,\"dpiY\":326},\"display\":{\"resolutionHeight\":647,\"resolutionWidth\":375},\"networkinfo\":{\"CONNECTION_TYPE\":3,\"CONNECTION_UNKNOW\":0,\"CONNECTION_NONE\":1,\"CONNECTION_ETHERNET\":2,\"CONNECTION_WIFI\":3,\"CONNECTION_CELL2G\":4,\"CONNECTION_CELL3G\":5,\"CONNECTION_CELL4G\":6},\"_plusready_dispatch\":\"Y\"}\n";
       System.out.println(res.contains("iOS"));
    }
    /**
     * 版本号比较
     *
     * @param v1
     * @param v2
     * @return 0代表相等，1代表左边大，-1代表右边大
     * Utils.compareVersion("1.0.358_20180820090554","1.0.358_20180820090553")=1
     */
    public static int compareVersion(String v1, String v2) {
        if (v1.equals(v2)) {
            return 0;
        }
        String[] version1Array = v1.split("[._]");
        String[] version2Array = v2.split("[._]");
        int index = 0;
        int minLen = Math.min(version1Array.length, version2Array.length);
        long diff = 0;

        while (index < minLen
                && (diff = Long.parseLong(version1Array[index])
                - Long.parseLong(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            for (int i = index; i < version1Array.length; i++) {
                if (Long.parseLong(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Long.parseLong(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }

}
